﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Reflection;
using System.ComponentModel;

namespace Catalyst.Web.DynamicData
{
    /// <summary>
    /// A parameter responsible for creating a LambdaExpression that performs a like comparisson on strings.
    /// </summary>
    public class LikeExpressionParameter : DynamicExpressionParameterBase
    {
        public enum LikeType
        {
            Contains,
            StartsWith,
            EndsWith
        }

        [DefaultValue("")]
        public string Value
        {
            get
            {
                object obj2 = this.ViewState["Value"];
                if (obj2 == null)
                {
                    return null;
                }
                return (string)obj2;
            }
            set
            {
                if (this.Value != value)
                {
                    this.ViewState["Value"] = value;
                    this.OnParameterChanged();
                }
            }
        }

        [DefaultValue("")]
        public LikeType Like
        {
            get
            {
                object obj2 = this.ViewState["Like"];
                if (obj2 == null)
                {
                    return default(LikeType);
                }
                return (LikeType)obj2;
            }
            set
            {
                if (this.Like != value)
                {
                    this.ViewState["Like"] = value;
                    this.OnParameterChanged();
                }
            }
        }

        public override LambdaExpression GetLambdaExpression(Type itType)
        {
            if (string.IsNullOrEmpty(Value))
                return null;

            MethodInfo methodInfo = typeof(string).GetMethod("Contains");

            if (Like == LikeType.StartsWith)
                methodInfo = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
            else if (Like == LikeType.EndsWith)
                methodInfo = typeof(string).GetMethod("EndsWith");

            ParameterExpression parameterExpression = Expression.Parameter(itType, "");
            return Expression.Lambda
                        (
                            Expression.Call
                            (
                                Expression.Property
                                (
                                    parameterExpression,
                                    this.Name
                                ),
                                methodInfo,
                                new Expression[] { Expression.Constant(Value, typeof(string)) }
                            ),
                            new ParameterExpression[] { parameterExpression }
                        );
        }

        
    }
}
